<template>
	<view class="page-body" :class="[app.setCStyle(from)]">
		<u-form :model="model"  ref="uForm" :errorType="errorType">
			<view class="pbox bg-white">
				<u-form-item :label-position="labelPosition" label="旧密码" prop="old_password" label-width="150">
					<u-input :password-icon="true" :border="border" type="password" v-model="model.old_password" placeholder="请输入旧密码"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="新密码" prop="password" label-width="150">
					<u-input :password-icon="true" :border="border" type="password" v-model="model.password" placeholder="请输入新密码"></u-input>
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="确认密码" prop="rePassword" label-width="150">
					<u-input :password-icon="true" :border="border" type="password" v-model="model.rePassword" placeholder="请输入确认密码"></u-input>
				</u-form-item>
				
			 
			</view>
			<view class="pbox mt40">
			<button size="default" shape="circle"  type="warning"  class="fs32 primarybtn" @click="submit">确认修改</button>
			</view>
		</u-form>
	</view>
</template>

<script>
	export default {
		data() {
			let that = this;
			return {
				from: '',
				model: {
					old_password: '',
					password: '',
					rePassword: '', 
				},
				pwrules: {
					old_password: [{
							required: true,
							message: '请输入旧密码',
							trigger: ['change', 'blur'],
						},
						{
							min: 8,
							message: '旧密码不能少于8个字符',
							trigger: 'change',
						}
					],
					password: [{
							required: true,
							message: '请输入新密码',
							trigger: ['change', 'blur'],
						},
						{
							min: 8,
							message: '新密码不能少于8个字符',
							trigger: 'change',
						}
					],
					rePassword: [{
							required: true,
							message: '请重新输入新密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.password;
							},
							message: '两次输入的密码不相等',
							trigger: ['change', 'blur'],
						}
					],
				},
				border: false,
				labelPosition: 'left',
				errorType: ['toast']
			};
		},
		onLoad(options) {
			this.from = options.from;
		},
		computed: {
			borderCurrent() {
				return this.border ? 0 : 1;
			},

		},
		onReady() {
			this.$refs.uForm.setRules(this.pwrules);
		},
		methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (!valid) {
						return false;
					} 
					this.$u.get('member/api.users/editPwd', this.model).then(res => {
						uni.showModal({
						    title: '提示',
						    content: '密码已重置，请使用新密码登录.',
							showCancel:false,
						    success: function (res) {
						        if (res.confirm) {
						           uni.redirectTo({
						           	url: '/pages/channel/passport/login'
						           });
						        } 
						    }
						});
					})
				});
			},
			
		}
	}
</script>

<style lang='scss'>

</style>
